package com.fishingwithme.android.ui.screens.map.handlers

import com.fishingwithme.android.R
import com.fishingwithme.android.utils.EventBus
import com.fishingwithme.android.utils.MapDataEvent

/**
 * 完成处理器
 */
class FinishHandler : BaseHandler() {
    override val key: String = "finish"
    override val icon: Int = R.mipmap.finish  // 使用"check_circle"图标表示完成操作

    override fun doAction() {
        super.doAction()
        finishOperation()
    }

    /**
     * 完成操作
     */
    private fun finishOperation() {
        try {
            // 打印数据以供调试
            android.util.Log.d("FinishHandler", "完成操作，数据: $data")

            // 通过事件总线发送数据
            data?.let {
                EventBus.postMapDataEvent(MapDataEvent(it))
                android.util.Log.d("FinishHandler", "已发送地图数据事件")
            }

            // 首先尝试直接调用导航回调
            if (mapContext != null && mapController?.onNavigateBack != null) {
                android.util.Log.d("FinishHandler", "调用直接导航回调")
                mapController?.onNavigateBack?.invoke("none")
            } else {
                android.util.Log.d("FinishHandler", "没有直接导航回调")
            }
        } catch (e: Exception) {
            android.util.Log.w("FinishHandler", "Error during finish operation", e)
        }
    }

    override fun reset() {
        super.reset()
        try {
            // Clean up all map elements through the context when finishing
            mapContext?.clearAllElements()
        } catch (e: Exception) {
            android.util.Log.w("FinishHandler", "Error during reset", e)
        }
    }
}